﻿
var count = 0;
$(document).ready(function () {
    toggoleLeftBoxGroup();
    $(".groupclose").hover(function () {
        var helper = $(this).find(".lm_helper");
        var hasNodes = $(this).next(".groupsub").children(".groupItem").length;
        helper.html(hasNodes == 0 ? "全部移回" : "全部添加");
        helper.show();
    }, function () {
        $(this).find(".lm_helper").hide();
    }); // end of .Group 


    $(".groupItem").live("click", function () {
        // alert($(this).html());
        $("#add_address").append($(this).html());
        $(this).remove();
        count = count + 1;
        $("#count_Receiver").text(count);
    });  //end of Item 

    $("#add_address a").live("click", function () {
        var innerDiv = $(this).find("div");
        var key = innerDiv.attr("key");
        var itemIndex = innerDiv.attr("itemIndex");

        //rebuild
        var buildBase = " <div class=\"groupItem\"><a class=\"lm_addr\" key=\"" + key + "\" href=\"javascript:void(0);" + "\"" + ">"
                + $(this).html() + "</a></div>";

        //alert(itemIndex);
        var leftDiv = $("#add_address_Left div[key=" + key + "]")
                        .next(".groupsub");
        var nextItem = itemIndex + 1;
        var beforeItem = itemIndex - 1;
        var leftDivSlibing_Before = leftDiv.find(".arguementBar[itemindex=" + beforeItem + "]"); //[itemindex=" + itemIndex - 1 + "]");
        var leftDivSilbing_After = leftDiv.find(".arguementBar[itemindex=" + nextItem + "]");

        //   alert(leftDivSlibing.attr("itemindex"));
        if (itemIndex == 0) {
            //头部 
            leftDiv.prepend(buildBase);
        } else {
            var after = leftDivSilbing_After.attr("itemindex");
            var before = leftDivSlibing_Before.attr("itemindex");

            //  alert(after);
            // alert(before);
            if (after == null && before == null) {
                //前面没有..后面也没有 
                //直接在最后插入 
                if (leftDiv.find(".groupMore").attr("key") == null) {
                    leftDiv.append(buildBase);
                } else
                    leftDiv.find(".groupMore").prepend(buildBase);
            }
            else if (after != null && before == null) {
                //在后面的前面插入 
                $(buildBase).insertBefore(leftDivSlibing_Before);
            } else if (after == null && before != null) {
                //  alert(buildBase);
                //在前面的后面插入
                leftDivSlibing_Before.parent().parent().after("" + buildBase + "");
            }
        }

        //  alert(buildBase);
        $(this).remove();
        count = count - 1;
        $("#count_Receiver").text(count);
    });
    toggleSearch();

    fosucTxt();
    $(".lm_helper").live("click", function () {
        var action = $(this).text();
        if (action == "全部添加") {
            $(this).parent().parent().next(".groupsub")
                    .children(".groupItem").each(function () {
                        $(this).click();
                    });
            $(this).text("全部移回");
        }
        else {
            var key = $(this).parent().parent().attr("key");

            $("#add_address").find("a[key=" + key + "]").each(function () { $(this).click(); });
            $(this).text("全部添加");
        }
    }); //end of .lm_helper_Click
    //groupMoreClick();

    //
    InitailCurrent();
});
// 设置选择联系人左侧"按钮"事件
function toggoleLeftBoxGroup() {
    $(".groupclose").click(function () {
        var isOpen = $(this).attr("open");
        var nextGroup = $(this).next(".groupsub");
        nextGroup.toggle();
        // close 
        if (isOpen == "false") {
            $(this).addClass("groupopen").removeClass("groupclose");
            $(this).attr("open", "true");
        }
        else {
            $(this).addClass("groupclose").removeClass("groupopen");
            $(this).attr("open", "false");
        }
    });
}
//
var isSearching = false;
function toggleSearch() {
    $("#btn_UserSearch").click(function () {
        var val = $.trim($("#txt_SearchText").val());

        var clsName = $(this).attr("class");

        if (clsName == "lm_sopen" && val != "查找联系人..." && val.length > 0) {
            isSearching = true;
        }
        else {
            isSearching = false;
            val = "";
        }

        if (isSearching) {
            //查找...
            $("#lb_SearchTips").show();
            $(this).addClass("lm_sclose").removeClass("lm_sopen");

            SearchItem(val);

        } else {
            $("#lb_SearchTips").hide();
            $(this).addClass("lm_sopen").removeClass("lm_sclose");
            $("#txt_SearchText").val("查找联系人...");
            val = "";
            InitialAll();
        }
    });
}
//搜索..
function fosucTxt() {
    $("#txt_SearchText").focus(function () {
        var val = $.trim($("#txt_SearchText").val());
        if (val == "查找联系人...") {
            $(this).val("");
            return false;
        }
    }).blur(function () {
        var val = $.trim($("#txt_SearchText").val());
        if (val.length == 0) {
            $("#txt_SearchText").val("查找联系人...");
            return false;
        }
    });
}
function btnCloseOkSelector_Click() {
    CallServerMethod("SubmitSelect$:$" + $("#add_address").html());
}

function Success(args, context) {

    if (args != "") {
        try {
            var isSearch = args.split('$:$');
            if (isSearch[0] == "Search") {
                $(".lm_panel").html(isSearch[1]);
            }
            else {
                var callBackResult = $.parseJSON(args);
                $("#txt_Receiver").val(callBackResult.users);
                $("#hf_CurrentSelected").val(callBackResult.Ids);
            }
        } catch (e) {
            alert(e);
        }

    } else  // lm_ifNullTips
    {
        $("#txt_Receiver").val("");
        return;
    }
}

//选中当前选择的.
function InitailCurrent() {
    var current = $("#hf_CurrentSelected").val().split(';');
    var length = current.length;
    for (var i = 0; i < length; i++) {
        $(".lm_panel .groupsub .groupItem").find("div[csid='" + current[i] + "']").click();
    }
}
